<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="套餐名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['taocan:setmeal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['taocan:setmeal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['taocan:setmeal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['taocan:setmeal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="setmealList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="套餐名称" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column
        label="套餐分类"
        prop="categoryName"
        align="center">
      </el-table-column>
      <el-table-column label="套餐价格" align="center" prop="price" />
      <el-table-column label="售卖状态">
        <template slot-scope="scope">
          <span>{{ scope.row.status == '0' ? '停售' : '启售' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后操作时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['taocan:setmeal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['taocan:setmeal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改套餐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="套餐名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="套餐分类:" prop="category_id">
          <el-select v-model="form.categoryId" placeholder="请选择套餐分类">
            <el-table-column :index="index+''" v-for="(item,index) in setmealList1" :key="index">
              <el-option :label="item.name" :value="item.id"></el-option>
            </el-table-column>
          </el-select>
        </el-form-item>
        <el-form-item label="套餐价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入套餐价格" />
        </el-form-item>
        <el-divider content-position="center">套餐菜品关系信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSetmealDish">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSetmealDish">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="setmealDishList" :row-class-name="rowSetmealDishIndex" @selection-change="handleSetmealDishSelectionChange" ref="setmealDish">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="菜品id" prop="dishId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dishId" placeholder="请输入菜品id" />
            </template>
          </el-table-column>
          <el-table-column label="菜品名称 " prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入菜品名称 " />
            </template>
          </el-table-column>
          <el-table-column label="菜品原价" prop="price" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" placeholder="请输入菜品原价" />
            </template>
          </el-table-column>
          <el-table-column label="份数" prop="copies" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.copies" placeholder="请输入份数" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sort" placeholder="请输入排序" />
            </template>
          </el-table-column>
          <el-table-column label="创建人" prop="createUser" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.createUser" placeholder="请输入创建人" />
            </template>
          </el-table-column>
          <el-table-column label="修改人" prop="updateUser" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.updateUser" placeholder="请输入修改人" />
            </template>
          </el-table-column>
          <el-table-column label="是否删除" prop="isDeleted" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isDeleted" placeholder="请输入是否删除" />
            </template>
          </el-table-column>
        </el-table>
        <el-form-item label="图片">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="套餐描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listSetmeal1,listSetmeal, getSetmeal, delSetmeal, addSetmeal, updateSetmeal } from "@/api/taocan/setmeal";

  export default {
    name: "Setmeal",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedSetmealDish: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 套餐表格数据
        setmealList: [],
        setmealList1: [],
        // 套餐菜品关系表格数据
        setmealDishList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          categoryId: null,
          name: null,
          price: null,
          status: null,
          code: null,
          description: null,
          image: null,
          createUser: null,
          updateUser: null,
          isDeleted: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "套餐名称不能为空", trigger: "blur" }
          ],
          price: [
            { required: true, message: "套餐价格不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getList1();
    },
    methods: {
      /** 查询套餐列表 */
      getList() {
        this.loading = true;
        listSetmeal(this.queryParams).then(response => {
          this.setmealList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getList1() {
        this.loading = true;
        listSetmeal1(this.queryParams).then(response => {
          this.setmealList1 = response;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          categoryId: null,
          name: null,
          price: null,
          status: 0,
          code: null,
          description: null,
          image: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          isDeleted: null
        };
        this.setmealDishList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加套餐";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSetmeal(id).then(response => {
          this.form = response.data;
          this.setmealDishList = response.data.setmealDishList;
          this.open = true;
          this.title = "修改套餐";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.setmealDishList = this.setmealDishList;
            if (this.form.id != null) {
              updateSetmeal(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSetmeal(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除套餐编号为"' + ids + '"的数据项？').then(function() {
          return delSetmeal(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 套餐菜品关系序号 */
      rowSetmealDishIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** 套餐菜品关系添加按钮操作 */
      handleAddSetmealDish() {
        let obj = {};
        obj.dishId = "";
        obj.name = "";
        obj.price = "";
        obj.copies = "";
        obj.sort = "";
        obj.createUser = "";
        obj.updateUser = "";
        obj.isDeleted = "";
        this.setmealDishList.push(obj);
      },
      /** 套餐菜品关系删除按钮操作 */
      handleDeleteSetmealDish() {
        if (this.checkedSetmealDish.length == 0) {
          this.$modal.msgError("请先选择要删除的套餐菜品关系数据");
        } else {
          const setmealDishList = this.setmealDishList;
          const checkedSetmealDish = this.checkedSetmealDish;
          this.setmealDishList = setmealDishList.filter(function(item) {
            return checkedSetmealDish.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleSetmealDishSelectionChange(selection) {
        this.checkedSetmealDish = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('taocan/setmeal/export', {
          ...this.queryParams
        }, `setmeal_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
